package com.ouc.openplatform.ouc.authManage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.ouc.openplatform.ouc.authManage.dto.NJ_DepartmentStructureTreeGridDto;
import com.ouc.openplatform.ouc.authManage.model.NJ_DepartmentStructure;
import com.ouc.openplatform.ouc.authManage.service.NJ_ClientDepartmentStructureService;
import com.ouc.openplatform.ouc.authManage.service.NJ_DepartmentStructureService;


public class NJ_ClientDepartmentStructureServiceImpl implements NJ_ClientDepartmentStructureService {
	private NJ_DepartmentStructureService departmentStructureService;

	public NJ_DepartmentStructureService getDepartmentStructureService() {
		return departmentStructureService;
	}

	public void setDepartmentStructureService(NJ_DepartmentStructureService departmentStructureService) {
		this.departmentStructureService = departmentStructureService;
	}

	// 1.保存部门信息-----sxl
	@Override
	public int saveDepartmentStructure(NJ_DepartmentStructure NJ_DepartmentStructure) {
		int flag = 0;
		// 判断部门名称是否存在
		int i = departmentStructureService.IsOkDepartment(NJ_DepartmentStructure.getDepartmentname(),
				NJ_DepartmentStructure.getDepartmentcode(), NJ_DepartmentStructure.getParentid());
		if (i == 0) {
			NJ_DepartmentStructure departmentStructure = new NJ_DepartmentStructure();
			departmentStructure.setDepartmentname(NJ_DepartmentStructure.getDepartmentname());
			departmentStructure.setDepartmentcode(NJ_DepartmentStructure.getDepartmentcode());
			departmentStructure.setParentid(NJ_DepartmentStructure.getParentid());
			departmentStructure.setAddress(NJ_DepartmentStructure.getAddress());
			departmentStructure.setType(NJ_DepartmentStructure.getType());
			departmentStructure.setAdduser(NJ_DepartmentStructure.getAdduser());
			departmentStructure.setDepth(NJ_DepartmentStructure.getDepth());
			departmentStructure.setAddtime(new Date());
			flag = departmentStructureService.saveDepartmentStructure(departmentStructure);
		} else {
			flag = 2;
		}
		return flag;
	}

	// 2.根据部门id删除部门信息------------sxl
	@Override
	public int deleteDepartmentStructure(String id) {
		int deptId = Integer.parseInt(id);
		// 根据部门id查询其子节点的个数
		int num = departmentStructureService.getDepStructureCountBypid(deptId);
		return num > 0 ? 2 : departmentStructureService.deleteDepartmentStructure(deptId);
	}

	// 3.修改部门信息-----sxl
	@Override
	public int updateDepartmentStructure(NJ_DepartmentStructure NJ_DepartmentStructure) {
		int flag = 0;
		// 判断部门名称是否存在
		int i = departmentStructureService.IsOkDepartment(NJ_DepartmentStructure.getDepartmentname(),
				NJ_DepartmentStructure.getDepartmentcode(), NJ_DepartmentStructure.getParentid());
		if (i == 0) {
			flag = departmentStructureService.updateDepartmentStructure(NJ_DepartmentStructure);
		} else {
			flag = 2;
		}
		return flag;
	}

	// 4.根据父级id查询所有的部门信息------sxl
	@Override
	public List<NJ_DepartmentStructureTreeGridDto> getDepInfosBypid(int id) throws Exception {
		List<NJ_DepartmentStructureTreeGridDto> treeDtoList = new ArrayList<NJ_DepartmentStructureTreeGridDto>();
		List<NJ_DepartmentStructure> list = departmentStructureService.getAllDepStructureBypid(id);

		for (NJ_DepartmentStructure departmentStructure : list) {
			// 创建一个节点
			NJ_DepartmentStructureTreeGridDto node = new NJ_DepartmentStructureTreeGridDto();
			BeanUtils.copyProperties(node, departmentStructure);
			node.setText(departmentStructure.getDepartmentname());
			node.setParent_id(departmentStructure.getParentid());
			// 根据父节点id查询子节点id的个数
			int count = departmentStructureService.getDepStructureCountBypid(departmentStructure.getId());
			node.setState(count > 0 ? "closed" : "open");
			treeDtoList.add(node);
		}
		return treeDtoList;
	}
	//5.获取所有部门信息-----sxl
	@Override
	public List<NJ_DepartmentStructure> getAllDepartmentStructure(NJ_DepartmentStructure NJ_DepartmentStructure) {
		List<NJ_DepartmentStructure> departmentStructureList = departmentStructureService.getAllDepartmentStructure(NJ_DepartmentStructure);
		return departmentStructureList;
	}

	@Override
	public List<NJ_DepartmentStructureTreeGridDto> getAllDepartmentStructureTree() {
	   return null;
	}

}
